(function($) {
	$.fn.extend({
		diyUpload:function(opt,serverCallBack) {
			if(typeof opt != "object") {
				alert('参数错误！');
				return;
			}
			var $fileInput = $(this);
			var $fileInputId = $fileInputId.attr('id');
			if(opt.url){
				opt.server = opt.url;
				delete opt.url;
			}
			if(opt.success){
				var successCallBack = opt.success;
				delete opt.success;
			}
			if(opt.error){
				var errorCallBack = opt.error;
				delete opt.error;
			}

			$.each(getOption('#'+$fileInputId),function(key,value){
				opt[key] = opt[key]|| value;
			});

			if(opt.buttonText){
				opt['pick']['label'] = opt.buttonText;
				delete opt.buttonText;
			}
			var webUploader = getUploader(opt);
			if(!WebUploader.Uploader.support()) {
				alert('上传的组件不支持您的浏览器！');
				return false;
			}
			//绑定文件加入队列事件
			webUploader.on('fileQueued',function(file){
				creatBox($fileInput, file,webUploader);
			});
			//进度条事件
			webUploader.on('uploadProgress',function(file,percerntage){
				var $fileBox = $ ('fileBox_'+file.id);
				var $diyBar = $fileBox.find('.diyBar');
				$diyBar.show();
				percentage = percentage*100;
				showDiyProgress(percentage.toFixed(2),$diyBar);
			});
			//全部上传结束后触发;
			webUploader.on('uploadFinished',function(){
				$fileInput.next('.parentFileBox').children('.diyButton').remove();
			});
			//绑定发送至服务器端返回后触发的事件
			webUploader.on('uploadAccept',function(object,data){
				if(serverCallBack) serverCallBack(data);
			});
			//上传成功后触发事件
			webUploader.on('uploadSuccess',function(file,response){
				var $fileBox = $('#fileBox_'+file.id);
				var $diyBar = $fileBox.find('.diyBar');
				$fileBox.removeClass('diyUploadHover');
				$diyBar.fadeOut(1000, function(){
					$fileBox.children('.diySuccess').show();

				});
				if(successCallBack){
					successCallBack(response);
				}
			});
			//上传失败后触发的事件
			webUploader.on('uploadError',function(file,reason){
				var $fileBox = $('#fileBox_'+file.id);
				var $diyBar = $fileBox.find('.diyBar');
				showDiyProgress(0, $diyBar ,'上传失败');
				var err = '上传失败！文件:' +file.name+'错误码:'+reason;
				if(errorCallBack) {
					errorCallBack(err);
				}
			});
			//选择文件错误触发事件
			webUploader.on('error',function(code){
				var text = '';
				switch(code){
					case 'F_DUPLICATE':text = '改文件已经被选择了！';
						break;
					case 'Q_EXCEED_NUM_LIMIT':text='上传文件的数量超过限制';
						break;
					case 'F_EXCEED_SIZE':text = '文件大小超过限制';
						break;
					case 'Q_EXCEED_SIZE_LIMIT':text = "所有的文件总大小超过限制";
						break;
					case 'Q_TYPE_DENIED' : text = '文件类型不正确或者是空文件';
						break;
					default : text = '未知错误！';
						break;
				}
				alert(text);
			});
			return webUploader;	
		}
	});
	function getOption(objId) {
		return{
			pick:{
				id:objId,
				label:""
			},
			accpet:{
				title:"Images",
				extensions:"gif,jpg,jpeg,bmp,png",
				mimeTypes:"image/*"
			},
			thumb:{
				width:160,
				height: 120,
				quality:90,
				allowMagnify:false,
				crop:true,
				type:"image/jpeg"
			},
			method:"post",
			server:"",
			sendAsBinary:false,
			chunked:true,
			chunkSize:512*124,
			fileNumLimit:50,
			fileSizeLimit:50,
			fileSizeLimit:500000*1024,
			fileSingleSizeLimit: 50000 *1024,
		};
		function getElementByUploader(opt){
			return new WebUploader.Uploader(opt);
		}
		//操作进度条
		function showDiyProgress( progress, $diyBar, text){
			if(progress>=100){
				progress = progress + %;
				text =text|| '上传完成';

			}else{
				progress = progress + '%';
				text = text || progress;
			}
			var $diyProgress = $diyBar.find('.diyProgress');
			$diyProgress.width(progress).text(text);
		} 
		//取消事件
		function removeLi ($li, file_id,webUploader){
			WebUploader.removeFile(file_id);
			$li.remove();
		}
		//左移事件
		function leftLi($leftli,$li) {
			$li.insertBefore($leftli);
		}
		//右移事件
	}
})